#!/usr/bin/python
# -*- coding: utf-8 -*-

try:
    from gluon.contrib.gql import *
except:
    db=SQLDB('sqlite://db.db')
else:
    db=GQLDB()
    session.connect(request,response,db=db)

db.define_table("config",
        SQLField("name"),
        SQLField("appkey"),
        SQLField("secret"),
        SQLField("url"),
        SQLField("v",'double')
        )
db.config.name.requires=[IS_NOT_EMPTY(), IS_NOT_IN_DB(db,'config.name')]
db.config.appkey.requires=IS_NOT_EMPTY()
db.config.secret.requires=IS_NOT_EMPTY()
db.config.url.requires=[IS_NOT_EMPTY(), IS_URL()]
db.config.v.requires=IS_NOT_EMPTY()

db.define_table("isp",
        SQLField("name"),
        )
db.define_table("system_a",
        SQLField("argument"),
        )
db.define_table("application_a",
        SQLField("argument"),
        )
db.define_table("api",
        SQLField("api"),
        )
db.define_table("type",
        SQLField("type",'integer'),
        )
db.define_table("interface",
        SQLField("isp",db.isp),
        SQLField("api"),
        SQLField("type",db.type),
        SQLField("sys_compulsory",'text'),
        SQLField("sys_optional",'text'),
        SQLField("app_compulsory",'text'),
        SQLField("app_optional",'text'),
        SQLField("explanation",'text'),
        )
db.isp.name.requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'isp.name')]
db.system_a.argument.requires=[IS_NOT_IN_DB(db,'system_a.argument'), IS_NOT_EMPTY()]
db.application_a.argument.requires=[IS_NOT_IN_DB(db,'application_a.argument'), IS_NOT_EMPTY()]
#db.api.api.requires=[IS_NOT_EMPTY(), IS_NOT_IN_DB(db,'api.api')]
db.type.type.requires=[IS_NOT_EMPTY(), IS_NOT_IN_DB(db,'type.type')]
db.interface.isp.requires=IS_IN_DB(db,'isp.id','isp.name')
db.interface.api.requires=IS_IN_DB(db,'api.api','api.api')
db.interface.type.requires=IS_IN_DB(db,'type.type','type.type')
db.interface.sys_compulsory.requires=IS_IN_DB(db,'system_a.id','system_a.argument',multiple=True)
db.interface.sys_optional.requires=IS_IN_DB(db,'system_a.id','system_a.argument',multiple=True)
db.interface.app_compulsory.requires=IS_IN_DB(db,'application_a.id','application_a.argument',multiple=True)
db.interface.app_optional.requires=IS_IN_DB(db,'application_a.id','application_a.argument',multiple=True)
